package org.budo.mail.javax.internet.message.builder;

import javax.mail.Address;
import javax.mail.Message.RecipientType;
import javax.mail.Session;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

/**
 * @author lmw
 */
public class MimeMessageBuilder {
    private MimeMessage mimeMessage;

    public MimeMessageBuilder(Session session) {
        this.mimeMessage = new MimeMessage(session);
    }

    public MimeMessageBuilder setFrom(String from) {
        try {
            mimeMessage.setFrom(new InternetAddress(from));
            return this;
        } catch (Throwable e) {
            throw new RuntimeException(e);
        }
    }

    public MimeMessageBuilder setReplyTo(String replyTo) {
        try {
            mimeMessage.setReplyTo(new Address[] { new InternetAddress(replyTo) });
            return this;
        } catch (Throwable e) {
            throw new RuntimeException(e);
        }
    }

    public MimeMessageBuilder setTo(String to) {
        try {
            mimeMessage.setRecipient(RecipientType.TO, new InternetAddress(to));
            return this;
        } catch (Throwable e) {
            throw new RuntimeException(e);
        }
    }

    public MimeMessageBuilder setSubject(String subject) {
        try {
            mimeMessage.setSubject(subject);
            return this;
        } catch (Throwable e) {
            throw new RuntimeException(e);
        }
    }

    public MimeMessageBuilder setContent(String content) {
        try {
            mimeMessage.setContent(content, "text/html;charset=UTF-8");
            return this;
        } catch (Throwable e) {
            throw new RuntimeException(e);
        }
    }

    public MimeMessage toMimeMessage() {
        return this.mimeMessage;
    }
}